#include "GLMessageBox.h"
//initializing the static variables
UIInputHandler GLMessageBox::inputHandler = NULL;
ClickHandler GLMessageBox::swapBuffers = NULL;
bool GLMessageBox::isDone = false;
int GLMessageBox::bId = -1;

GLMessageBox::GLMessageBox(int x, int y, int w, int h)
    :GLRect(x, y, w, h, 10)
{
}

GLMessageBox::~GLMessageBox()
{
}

void GLMessageBox::b0Pressed()
{
    bId = 0;
    isDone = true;
}
void  GLMessageBox::b1Pressed()
{
    bId = 1;
    isDone = true;
}
void  GLMessageBox::b2Pressed()
{
    bId = 2;
    isDone = true;
}
void  GLMessageBox::setCallbacks(UIInputHandler inh, ClickHandler swp)
{   
    inputHandler = inh;
    swapBuffers = swp;
}
int  GLMessageBox::information(char *caption, char *text, char *b0Text, char *b1Text, char *b2Text)
{
    vector <GLButton *> buttons;
    bId = -1;
    isDone = false;
    GLRect *top = new GLRect(50, HEIGHT-50, WIDTH-100, 50, 10);
    GLRect *base = new GLRect(50, HEIGHT-50, WIDTH-100, HEIGHT-100, 10);
    TextObject *cap = new TextObject(g_GLuiFont);

    vector <TextObject*> mesgs;
    string str(text);
    int numChars = base->getWidth()/g_GLuiFont->size; 
    int numSupported = strlen(text) / numChars; 

    for(int i = 0, index = 0; index < strlen(text); i++)
    {
        int end = index + numChars;
        string tmpStr;

        if(end > strlen(text))
        {
            end = strlen(text);
        }
        else
        {
            if(text[end] != ' ')
            {
                while(text[--end] != ' ')
                {
                    if(end <= index) //this doesn't seem like a valid sentence
                    {
                        end = index + numChars;
                        break;
                    }
                }
            }
            end++;
        }
        tmpStr = str.substr(index, end-index);
        index = end;
        //printf("%s\n", tmpStr.c_str());
        mesgs.push_back(new TextObject(g_GLuiFont));
        mesgs[i]->setText(tmpStr.c_str());
        mesgs[i]->setColor(0, 0, 0);
    }


    cap->setText(caption); 
    cap->setColor(255, 255, 255);
    base->setColor(150, 150, 150);
    top->setColor(20, 90, 150);

    if(b0Text)
    {   
        //add button with the give label
        buttons.push_back(new GLButton(base->getWidth()/2.0 - 150, 100, inputHandler, b0Pressed, b0Text));
    }
    else
    {
        buttons.push_back(new GLButton(base->getWidth()/2.0f, 100, inputHandler, b0Pressed, (const char *)"OK"));
    }

    if(b1Text)
    {
        buttons.push_back(new GLButton(buttons[0]->getWidth() + buttons[0]->getX() + 50, 100, inputHandler, b1Pressed, b1Text));
    }

    if(b2Text)
    {
        buttons.push_back(new GLButton(buttons[1]->getWidth() + buttons[1]->getX() + 50 , 100, inputHandler, b2Pressed, b2Text));
    }

    if(b0Text && b1Text && !b2Text) //only two buttons
    {
        buttons[0]->move(buttons[1]->getX()-100, buttons[0]->getY());
        buttons[1]->move(buttons[1]->getX()+100, buttons[1]->getY());
    }

    glDisable(GL_DEPTH_TEST);
    while(!isDone)
    {
        glClear(GL_COLOR_BUFFER_BIT);
        int x = -1, y = -1;
        bool clicked;
        //get the cursor position
        inputHandler(x, y, clicked);
        if(x == -1 && y == -1)
        {
            //this indicates that InputHandler got ESC key event.
            bId = -1; 
            isDone = true;
        }
        glDisable(GL_TEXTURE_2D);
        base->draw();
        top->draw();
        glEnable(GL_TEXTURE_2D);

        //draw the messagebox
        cap->drawText(top->getX() + top->getWidth()/2.0 - cap->getW()/2.0, HEIGHT-60);
        //draw the information text
        for(unsigned int i = 0; i < mesgs.size(); i++)
        {
            mesgs[i]->drawText(top->getX() + top->getWidth()/2.0 - mesgs[i]->getW()/2.0,
                    top->getY()-top->getHeight() - 100 - i * g_GLuiFont->size);
        }
        //draw the buttons
        for(unsigned int i = 0; i < buttons.size(); i++)
        {
            buttons[i]->show();
        }

        //draw cursor
        glColor4f(1, 1, 1, 1);
        GLui::drawCursor(x, y, g_GLuiCursorTex->GetTexID());

        swapBuffers();
    }
    glEnable(GL_DEPTH_TEST);
    delete cap;
    delete top;
    delete base;
    for(unsigned int i = 0; i < mesgs.size(); i++)
        delete mesgs[i];
    mesgs.clear();

    for(unsigned int i = 0; i < buttons.size(); i++)
    {
        delete buttons[i];
    }
    buttons.clear();
    return bId;
}


